package com.zycfc.zsf.boot.redis;

import com.zycfc.zsf.boot.serializer.*;
import org.apache.commons.lang3.*;
import redis.clients.jedis.*;
import org.slf4j.*;

public class RedisStandaloneUtil
{
    private static final Logger log;
    private static JedisPool jedisPool;
    private static Integer MAXTOTAL;
    private static Integer MAXIDLE;
    private static Integer MAXWAITMILLIS;
    private static Boolean TESTONBORROW;
    private static Boolean TESTONRETURN;
    private static Integer TIMEOUT;
    private static int DATABASE;
    private static String namespace;
    private static String KEY_CHAR_SET;
    private static ISerializer serializer;
    
    public static <T> T get(final String key) {
        if (StringUtils.isEmpty((CharSequence)key)) {
            throw new IllegalArgumentException("key is required.");
        }
        final Object obj = null;
        try {
            final long begin = System.currentTimeMillis();
            final String fullKey = key(key);
            final byte[] realKey = fullKey.getBytes(RedisStandaloneUtil.KEY_CHAR_SET);
            final Jedis jedis = getJedis();
            final T result = (T)RedisStandaloneUtil.serializer.deserialize(jedis.get(realKey));
            final long end = System.currentTimeMillis();
            RedisStandaloneUtil.log.info("\u672c\u6b21get\u82b1\u8d39\u65f6\u957f{}ms----begin:{},end:{}", (Object)(end - begin));
            return result;
        }
        catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
    
    public static synchronized Jedis getJedis() {
        try {
            if (RedisStandaloneUtil.jedisPool != null) {
                final Jedis resource = RedisStandaloneUtil.jedisPool.getResource();
                return resource;
            }
            return null;
        }
        catch (Exception e) {
            RedisStandaloneUtil.log.error("\u4ecejedisPool\u83b7\u53d6resource \u53d1\u751f\u5f02\u5e38\uff1a", (Throwable)e);
            return null;
        }
    }
    
    private static String key(final String key) {
        if (null == key || key.length() == 0) {
            throw new IllegalArgumentException("key is empty!");
        }
        return RedisStandaloneUtil.namespace + ":" + key;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RedisStandaloneUtil.class);
        RedisStandaloneUtil.MAXTOTAL = 5000;
        RedisStandaloneUtil.MAXIDLE = 50;
        RedisStandaloneUtil.MAXWAITMILLIS = 2000;
        RedisStandaloneUtil.TESTONBORROW = false;
        RedisStandaloneUtil.TESTONRETURN = false;
        RedisStandaloneUtil.TIMEOUT = 2000;
        RedisStandaloneUtil.DATABASE = -1;
        RedisStandaloneUtil.namespace = null;
        RedisStandaloneUtil.KEY_CHAR_SET = "UTF-8";
        RedisStandaloneUtil.serializer = null;
    }
}
